Skip to content

Instantly share code, notes, and snippets.

@mkfares
Last active July 29, 2020 10:10
Show Gist options
  • Save mkfares/b75dbec3e6207987819365ecfbc317db to your computer and use it in GitHub Desktop.
Save mkfares/b75dbec3e6207987819365ecfbc317db to your computer and use it in GitHub Desktop.
Docker Managing Containers

Managing Docker Containers

Most of tasks that are related to the management of containers are performed using the command: docker container. However, some commands process a short notation.

A container or an image can be mentioned by its name (see option --name) or by its id. The container id can be shortened to the few first unique characters.

Create new containers

The container is created but not started. This helps speedup the starting and running of containers.

$ docker container create [options] <image> [command]

Options:
-e : Set environment variables
-i : Run container in interactive mode
--name : Assign a name to the container
-p : publish container's ports to the host (-p 8080:80 host:container)
--rm : Remove the container after it exits
-t : Allocate a terminal to the container
-v : Bind a volume to the container

Command: The command with its arguments to be executed in the container. For instance, /bin/sh -c "echo hello"

List containers

$ docker ps
$ docker ps -a
$ docker container ls -a

Options:
-a : Show all containers including the stopped ones
-q : Show container numeric ids

Start Containers

$ docker container start [options] <container>
$ docker start <container>

Options:
-a : Attach stdout and stderr to the container
-i : Attach stdin to the container

Stop containers

$ docker container stop <container>
$ docker stop <container>

Restart containers

$ docker container restart <container>

Attach streams to containers

The streams to be attached are stdin, stdout, and stderr. The container should be running to attach to it.

$ docker container attach <container>  
$ docker attach <container>

CTRL-c: Stop the container
CTRL-p CTRL-q: Detach from the container while leaving it running

Inspect changes made to containers

This command lists the files and directory that were added (A), deleted (D) or changed (C) in the container.

$ docker diff <container>

Execute commands in running containers

The container should be running to execute commands.

$ docker run --name malpine -it --rm alpine sh 
$ docker exec [options] malpine touch demo.txt

Inspect containers

$ docker container inspect <container>

Show container logs

$ docker container logs [options] <container>
$ docker logs [options] <container>

Options:
-f : Follow log output
-t : Show timestamps

Remove all stopped containers

$ docker container prune [options]

Options:
-f : Delete without confirmation

Remove containers

$ docker container rm [options] <container>

Options:
-f : Remove container without confirmation
-v : Remove the associated volumes with a container

Run containers

The run command is the combination of the create and start commands.

$ docker container run [options] <image> [command]
$ docker run [options] <image> [command]

Options:
-a : Attach container to stdin, stdout and stderr
-d : Run container in detach mode (in background)
-e : Set environment variables for container
-i : Run container in interactive mode (open stdin)
--name : Assign a name to the container
-p : Assign a host port to a container port (-p 8080:80)
--rm : Remove a container after it exits
-t : Assign a terminal to a container (for container input and output)
-v : Bind mount a volume to container

Command: The command to be executed in the container after it starts. The command may have arguments.

Examples:

$ docker run -it --rm --name myname <image> /bin/bash
$ docker run -d -p 8080:80 <image> 
docker run -a stdin -a stdout -it <image> /bin/bash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment