Docker Container

Learn how to work with Docker Container

Running containers

Docker run command is used to run a container.

syntax

1
docker run [OPTION] image_name

A docker container can be referenced by its name or id. You can provide a name as the Container name, if you don’t provide a name, Docker will generate one for you.

Option Description
--name name of the container
--rm Automatically remove the container when it exits
-d Run container in background
-it interactive mode
-p host_port:container_port bind container port to host port
-v bind a volume

For more info see docker run reference

Run in background

use -d flag to run the container in the background. d is for detached.

1
docker run -d --rm redis

Interactive Mode

The -it instructs Docker to allocate a pseudo-tty connected to the container’s stdin. Both instructions are needed to allocate a pseudo-tty. If you leave out either one, the tty you allocate will not work normally.

  • -i : Keep STDIN open even if not attached
  • -t : Allocate a pseudo-tty

creating an interactive bash shell to the container.

1
docker run --rm -it ubuntu /bin/bash

Container Name

1
docker run --name my-httpd -p 8080:80 -d nginx:alpine

hostname

use –hostname option to set the hostname. If you don’t set the hostname, the default hostname wil be the container id

1
docker run --rm -it --name=ubuntu --hostname=myubuntu ubuntu:latest /bin/bash

Port Binding

Format

1
ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort | containerPort

start up an httpd container in the background and bind container’s port 80 to host’s port 8080. By default, the port is bound to all interfaces(0.0.0.0)

1
docker run --rm -p 8080:80 -d nginx:alpine

expose to local only(127.0.0.1)

1
docker run --rm -p 127.0.0.1:8080:80 -d nginx:alpine

expose to global(0.0.0.0)

1
docker run --rm -p 0.0.0.0:8080:80 -d nginx:alpine

Mount volumes

Volumes are used to presist data generated by and used by Docker container. Volume is managed by Docker.

1
docker run -dit --rm  --name myNginx -v myvol:/usr/share/nginx/html nginx:alpine

if myvol not exist, then Docker creates it

Bind Mounts

see Bind Mounts reference. a file or directory on the host is mounted into a container. Bind Mounts is similar to Volumne, but it is not managed by Docker.

use -v to mount host’s ~/nginx directory to continer’s /usr/share/nginx/html direcotry

1
docker run --name myNginx -v ~/nginx:/usr/share/nginx/html -p 8000:80 -d nginx:alpine

Resource Limit

limit the container’s memory to 900M. You can limit on memory, CPU and IO

1
docker container run -it  --memory 900m  alpine sh

Setting restart policy

use --restart flag to specify a restart policy

  • no - do not automatically restart the container when exit. this is the default
  • on-failure[:max-retries] - restart only if the container exits with a non-zero exit status
  • always - always restart the container regardless of exit status
  • unless-stopped - Always restart the container regardless of the exit status, including on daemon startup, except if the container was put into a stopped state before the Docker daemon was stopped.

Example

1
docker run --restart=always redis

List Containers

use docker ps command to list running containers

sample output

1
2
CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS              PORTS                  NAMES
006638797f61 httpd "httpd-foreground" 4 minutes ago Up 4 minutes 0.0.0.0:8080->80/tcp my-httpd

use docker ps -a command to list all containers including the stopped ones.

sample output

1
2
3
4
CONTAINER ID        IMAGE               COMMAND              CREATED             STATUS                     PORTS                  NAMES
af18818849b4 ubuntu "/bin/bash" 6 seconds ago Exited (0) 3 seconds ago inspiring_curran
006638797f61 httpd "httpd-foreground" 5 minutes ago Up 5 minutes 0.0.0.0:8080->80/tcp my-httpd

We can add -q option to list only the IDs of all containers

1
docker ps -q -a

It is very useful when we need to remove all containers

1
docker rm -f $(docker ps -a -q)

Stop Container

Command Description
docker stop container_name/container_id stop a container
docker kill container_name/container_id kill a container
docker restart container_name/container_id restart a container

For docker stop , The main process inside the container will receive SIGTERM, and after a grace period, SIGKILL. docker stop is more graceful than docker kill.

For docker kill, the main process receive SIGKILL signal.

docker stop attempts a graceful shutdown while docker kill is just a
after a container is stop, you can use docker start command to start it.

1
docker start container_name/container_id

Remove Contaienr

remove a stopped container using docker rm command.

1
docker rm container_name/container_id
Options Description
–force , -f Remove container regardless of container status.
–volumes , -v Remove the volumes associated with the container

docker container prune - remove all stopped container

Inspect container

Return low-level information on Docker objects

1
docker inspect container_name/container_id

if you only want to get specific info, use --format, -f option

To get log path

1
docker inspect --format='{{.LogPath}}'container_name/container_id

To get IP address. You can actually interact with the container using the IP address.

1
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name/container_id

docker exec command

use docker exec command to execute command on a running container. Usually it is used to start sh/bash session

Example

1
docker exec -it mycontainer /bin/bash

Logs

Show container logs

1
docker logs container_name/container_id

Show container logs with follow

1
docker logs -f container_name/container_id

Show only the latest log

1
docker logs --tail 5 container_name/container_id

stats

use docker stats <container> to display a live stream of containers resource usage.

Sample output

1
2
CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
12d75ef956ab ubuntu 0.00% 2.484MiB / 7.694GiB 0.03% 6.36kB / 0B 0B / 0B 1

Attach

attach - attach local standard input, output, and error streams to a running container.

You can detach from a container and leave it running using the CTRL-p CTRL-q key sequence. Do not use CTRL-c or exit command to detach from a container.

1
docker attach container-name

Attach vs exec

The difference between docker attach and docker exec -it <container> /bin/bash is docker exec creates a new process while docker attach attaches to the existing process that is running.

Other commands

Restart containe

1
docker restart container_name/container_id

Copy files/folders between a container and the local filesystem

1
2
3
$ docker cp ubuntu:/etc/hosts /tmp

$ docker cp /tmp/sample.txt ubuntu:/home

Reference