DEV Community

Manoj Kumar Patra
Manoj Kumar Patra

Posted on • Edited on

Docker Cheat sheet - Installation and Docker Containers

Installation

Install Docker on Linux:

curl -fsSL https://get.docker.com -o get-docker.sh
Enter fullscreen mode Exit fullscreen mode

To use Docker as a non-root user, consider adding the user to the docker group:

sudo usermod -aG docker <username>
Enter fullscreen mode Exit fullscreen mode

Download docker-compose:

sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

sudo chmod +x /usr/local/bin/docker-compose
Enter fullscreen mode Exit fullscreen mode

Check docker-compose version:

docker-compose version
Enter fullscreen mode Exit fullscreen mode

TIP: For auto-completion, docker + Tab Key

Versions

Docker versions are YY.MM based.

Edge -> monthly release and one month support
Stable -> quarterly release and 4 months support

Containers

Check version of client and server (engine):

docker version
Enter fullscreen mode Exit fullscreen mode

More information about the client and server:

docker info
Enter fullscreen mode Exit fullscreen mode

Management commands

docker <command> <sub-command> (options)
Enter fullscreen mode Exit fullscreen mode

Image vs. Container

Image is the app to run
Container is an instance of that image running as a process. There can be many containers.

Docker's default image registry is Docker Hub.

Starting an Nginx server

Download an image from Docker Hub and start a container from that image:

docker container run --publish 80:80 nginx
Enter fullscreen mode Exit fullscreen mode

The command docker container run does the following:

  1. Looks for the image locally in the image cache
  2. If not found locally, it looks for the same in the remote image repository which defaults to Docker Hub and downloads the latest version
  3. Creates a container based on that image and prepares to start
  4. Gives the container a virtual IP on a private network inside the Docker engine
  5. Opens up port 80 on the host and forwards all requests to port 80 in the container
  6. Starts container by using the CMD instruction command in the image's Dockerfile

💡 Instruction commands

There are three types of instructions (commands) that we can use to build and run Dockerfiles:

  1. RUN - Mainly used to build images and install applications and packages. Builds a new layer over an existing image by committing the results.

  2. CMD - Sets default parameters that can be overridden from the Docker CLI when a container is running.

  3. ENTRYPOINT - Default parameters that cannot be overridden when Docker Containers run with CLI parameters.

Running container in the background:

docker container run --publish <port-host>:<port-container> --detach <image>

# Example
docker container run --publish 80:80 --detach nginx
Enter fullscreen mode Exit fullscreen mode

Give a name to the container:

docker container run -p 80:80 -d --name webhost nginx
Enter fullscreen mode Exit fullscreen mode

-p is short for --publish and -d is short for --detach

List all containers:

docker container ls -a
Enter fullscreen mode Exit fullscreen mode

Short version:

docker ps -a
Enter fullscreen mode Exit fullscreen mode

List only running containers:

docker container ls
Enter fullscreen mode Exit fullscreen mode

Short version:

docker ps
Enter fullscreen mode Exit fullscreen mode

Stop a container:

docker container stop <first-3-digits-container-id>
Enter fullscreen mode Exit fullscreen mode

Get logs for a running container:

docker container logs <container-name>
Enter fullscreen mode Exit fullscreen mode

Display the running processes of a container:

docker container top <container-name>
Enter fullscreen mode Exit fullscreen mode

Remove one or more containers:

docker container rm <space-separated-3-digit-container-ids>
Enter fullscreen mode Exit fullscreen mode

Force remove running containers:

docker container rm -f <space-separated-3-digit-container-ids>
Enter fullscreen mode Exit fullscreen mode

Show all running processes:

ps aux
Enter fullscreen mode Exit fullscreen mode

Use environment variables:

docker container run --publish 3307:3307 --detach --name database --env MYSQL_RANDOM_ROOT_PASSWORD=yes mysql
Enter fullscreen mode Exit fullscreen mode

Get details of container config (metadata in JSON format):

docker container inspect <container-name>
Enter fullscreen mode Exit fullscreen mode

Get live performance stats for all containers:

docker container stats
Enter fullscreen mode Exit fullscreen mode

Get performance stats for a container:

docker container stats <container-name>
Enter fullscreen mode Exit fullscreen mode

TIP - To see all options for a command, do docker <command> <sub-command> --help

Get a shell inside containers

Start a new container interactively:

docker container run -it --name <container-name> <image> <cmd>

# Example
docker container run -it --name nginx nginx bash
Enter fullscreen mode Exit fullscreen mode
# Default CMD for Ubuntu image is bash
docker container run -it --name ubuntu ubuntu
Enter fullscreen mode Exit fullscreen mode

-it is two different options:
-t -> allocates Pseudo-TTY, simulates a real terminal
-i -> keeps interactive session open to receive terminal input

Start an existing container (stopped earlier) interactively:

docker container start -ai <container-name>
Enter fullscreen mode Exit fullscreen mode

-a -> attach
-i -> interactive shell

Run additional command in an existing running container:

docker container exec -it <container-name> <cmd>
Enter fullscreen mode Exit fullscreen mode

Download an image:

docker pull <image>
Enter fullscreen mode Exit fullscreen mode

List all images:

docker image ls
Enter fullscreen mode Exit fullscreen mode

Top comments (0)