Skip to main content

Containers

Docker containers are instances that run virtual machines along with the desired software applications within them. In a microservice architecture, each service runs separately through Docker containers.

Create a docker container

To create docker container we must know the docker image you will learn more about docker images here, but for now lets assume our docker image name is hello-world then bellow are few commands related to containers

docker run

The docker run command will create a container out of given docker image. See below example commands


docker run <image> # syntax - I
docker run hello-world # example - I.a
docker run hello-world:latest # example - I.b
docker run hello-world:1.0.2 # example - I.c

docker container run <image> # syntax - II
docker container run hello-world # example - II.a
docker container run hello-world:latest # example - II.b
docker container run hello-world:1.0.2 # example - II.c

To run a container as per old syntax the command can start with docker run, but after new standardized syntax it should be docker container run. Though

--publish (-p)

The --publish option used to publish given ports to host machine. The -p is shorthand of --publish option. below code will explain how to use the option.

docker run --publish <host-port>:<container-port> <image> # can also start with docker container run
docker run --publish 8080:80 nginx:latest

docker run --p <host-port>:<container-port> <image>
docker run --p 8080:80 nginx:latest

Though the docker container run is more standardized way of running container over docker run, the docker run is more popular.

--name

The --name option will allow you to provide a name to container.

--interactive (-i)

The --interactive option used to keep STDIN open, i.e to connect the terminal and see live logs.

--tty (-t)

The --tty option allocates a pseudo-tty, it helps to connect container bash in interactive mode.

--add-host

The --add-host option used to enable docker container access to host network, once added the container can access host machin through ip

docker run --p <host-port>:<container-port> --add-host host.docker.internal:host-gateway <image> 
docker run --p 8080:80 --add-host host.docker.internal:host-gateway nginx:latest

--rm

The --rm option will remove the container immediately after container exit(stop).

Below is example on how we can use all of them to gether

docker run --name my-server -it --rm  -p 7000:80 nginx:latest
  • The above command will create a new container with name my-server
  • The command will start in interactive and tty mode
  • By default when nginx server starts it starts on port 80 inside the container, but can't be accessed by host machine. The -p(--publish) command will open port 80 with 7070 to host machine.
  • The host machine can access the nginx with http://localhost:7070 link.

--volume (-v)

The volume option enables you to use host machine folder inside the docker container, when you run a container that stores data (like database applications), as soon as container removed the data would be lost. This will also enables you to update content dynamically while container is running.

docker run -it -p <host port>:<instance port> -v <local path>:<docker path> --name <name> <image>
docker run -it -p 3000:80 -v /Users/knowbasics/workspace/my-website/.:/Usr/src/app --name hello-world mynode-server

In above command the mynode-server is my custom node.js based application, the above command will run the server and uses the my-website folder to run application.

There are many other options are available to use, you can find them from the offical website here

docker exec

The docker exec or in standardized way docker container exec is the command that allows to execute commands on running docker containers. For example you can connect to bash of the container, or set environment variables, change working directory etc.

Bellow are the possible options to use along with docker exec command

  • --detach (-d) : - Detach mode, the command that executed will run in background.
  • --env (-e) : - Sets environment variables
  • --env-file : - Read environment variables from the given file
  • --interactive (-i) : - The interactive mode, works same as explained above
  • --tty (-t) : - The tty mode
  • --privileged : - Give extended privileges to the command
  • --user (-u) : - User name or UID used to execute the command
  • --workdir (-w) : - Set working directory inside the container.

docker exec -it mycontainer sh # connects the containers terminal
docker exec -it mycontainer bash # connects the containers terminal

docker exec -e ENV_VAR=1 mycontainer env

docker attach

The docker attach command allow you to reconnect to same session of container terminal

docker attach ubuntu-test

docker start

This command is used to start an existing container

docker stop

This command is used to stop a running container

docker restart

This command is used to restart a running container, if container is not already running, it will still start the container.

docker start <instance id | container name>
docker stop <instance id | container name>
docker restart <instance id | container name>

docker stop my-web-server
docker start my-web-server
docker restart my-web-server

docker ps

This command is used to show list of containers created in your host machine, by default it will show only list of running containers, but by passing flat -a the command will show all list that includes stopped containers.

docker ps # shows only list of running containers
docker ps -a # shows list of all containers

docker rm

Deletes the docker container, the default command will delete container only if it is in stopped mode, to delete running instance we have to force it by adding -f flag.

docker prune

Removes all stopped containers

docker rm <instance id | container name>
docker rm -f <instance id | container name>

docker prune # removes all stopped containers

docker rm my-web-server # deletes the container only if it is already stopped or throws the error
docker rm -f my-web-server # force to delete the container even it is running.

docker rm -f $(docker ps -a -q) # Forcefully stops and deletes every container.

docker stats

This command prints statistics of docker container bellow are examples on how it looks.

docker stats <container name>

#Below are while container running
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
00db63ea8ff1 my-web-serv 0.04% 112.7MiB / 7.676GiB 1.43% 876B / 0B 55.2MB / 4.1kB 19

#Below are while container in stop mode
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
00db63ea8ff1 my-web-serv 0.00% 0B / 0B 0.00% 0B / 0B 0B / 0B 0

docker inspect

This command will provide detailed information of the docker container in JSON mode. The output is in JSON mode and will have many details, bellow screenshot shows the JSON in collapsed mode

docker inspect <container name>

docker inspect results

docker logs

This option will print logs of the given containers

docker logs <container name>

docker top

This command will print information of services that are running inside the given container

docker top <container name>
docker top my-db

# shows bellow result.
UID PID PPID C STIME TTY TIME CMD
999 3773 3747 0 05:47 ? 00:00:07 mariadbd