Docker 101 : Docker basic commands 2



Running a docker container from an image:


The image will be downloaded from the docker hub if the image does not exist on the host.
Docker pulls images layer per layer, if a layer already exists on the host it does not pull it from the docket hub.

Pulling an image from the docker hub without running it:


Listing all the running containers:


We could also use the most recent version of the command:


Listing the latest created container:


Or with the most recent version of the command:



Listing all the containers (running and stopped ):


Or the more recent way of doing it:



Stopping a running container:


Remark:

We could use the container name or the container ID.

Listing docker images on the machine:


Or:

Removing an images:


The containers using this image need to be stopped before we can delete it (the image) using the above command.

Executing a command inside a running container:


Runs the command "cmd" inside a running container.
We could use the container name or the container ID that we can get by running the "docker container ls" command.

Running a container in a detached mode:


Runs the container in the background and we get the system prompt.

Removing a container:

We can't remove a running container, we need to stop it first using the below command:


Then, we can remove it using the below:


Running a container in the background:


Runs a container in the background in a detached mode.
We can bring it back to the "foreground" using the "attach" command as below:


The above command reattaches the container to the standard output.

Running a image in an interactive mode:


  • -i: interactive mode.
  • -t: attaches the container to the standard output.
We use the option for interactive programs (bash shell or a program that requires input for example. The "-t" attaches the application running inside the container to the terminal.

Port mapping: 


Maps a port on the docker container to a free port on the host:


Maps the host_port on the host to container_port on the container.
We can access the application in the container, using the "ip_of_host:host_port" combination.
Traffic to the "host_port" port will be routed to "container_port"  port on the container.

Volume mapping:


We use the below command to map a folder in the host to a folder in the docker container:


When the container runs, it will mount the host folder on the folder in the container.
When the container exits or crashes, all the data will be safe on the host folder.
All the data will be stored in the host directory.

Getting information about the container:


We could use the "container name"
 or the "container ID".

Reading a container logs:



Comments

Leave as a comment:

Archive