Docker Basics

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Docker

Images

Pulling images

To pull an image: docker pull <image>


To see the images: docker images
To remove an image: docker image rm <image_id> or docker rmi <image_id>

Containers

Running containers

To run a container of an image: docker run <image>:<tag>


If we don't specify a tag, we run the "latest" by default
To see running containers: docker container ls or docker ps
Run a container in detached mode: docker run <image> -d
Stop a container: docker stop <container_id|container_name>

Exposing ports

Run a container and map the host port to the container's port: docker run -p <host_port>:<container_port>
<image>
Map multiple ports: docker run -p <host_port_1>:<container_port_1> -p <host_port_2>:
<container_port_2> <image>

Managing containers

To start a stopped container: docker start <container_name|container_id>


List of all containers: docker ps -a
To remove containers: docker rm <container_id|container_id>
To remove all containers (only works if all containers are not running): docker rm $(docker ps -aq)
To remove all containers forcibly: docker rm -f $(docker ps -aq)

Naming containers

To name a container: docker run --name <your_container_name> <image>

Formatting the output

To format the output of docker ps : https://docs.docker.com/engine/reference/commandline/ps/#formatting

Volumes
Create a volume: docker run -v <path_on_host>:<path_on_container>:<options> <image>
Options can be:
ro: read only
Go inside a container: docker exec -it <container_id|container_name> <command>

Sharing volumes between containers


Share a volume from an existing container: docker run --volumes-from <container_name|container_id>
<image>

Dockerfile

Example of a Dockerfile:

FROM nginx:latest
ADD . /usr/share/nginx/html

Building Dockerfile

To build an image: docker build --tag <image_name>:<tag> .

Dockerfile for basic Express API

FROM node:latest
# If there is a folder called "app" use it, otherwise create a new one
WORKDIR /app
ADD . .
RUN npm install
CMD node index.js

To see the difference between RUN and CMD go here: https://goinbigdata.com/docker-run-vs-cmd-vs-


entrypoint/#:~:text=In%20a%20nutshell,will%20run%20as%20an%20executable.

.dockerignore

Example:

node_modules
Dockerfile
.git

Caching
Suppose we have this Dockerfile :

FROM node:latest
WORKDIR /app
ADD . .
RUN npm install
CMD node index.js

It can be improved like this:


FROM node:latest
WORKDIR /app
ADD package*.json ./
RUN npm install
ADD . .
CMD node index.js

Alpine
To get latest alpine version of an image (works almost on all cases): docker pull <image>:alpine

Tags, Versioning and Tagging


Allows you to control image version
Avoids breaking changes
Safe

Example:

FROM nginx:1.17.2-alpine

Tag an image

docker tag <image>:latest <image>:<tag>

Docker registries
Highly scalable server side application that stores and lets you distribute Docker images
Used in your CI/CD pipeline
Run your applications

Examples: * Docker Hub * quay.io * Amazon ECR

To push an image you first need to login with docker login . Then you have to docker push according to the instructions
given by Docker Hub or other container registry service.

Docker inspect
To inspect a container: docker inspect <container_id>

Docker logs
To see the logs of a container: docker logs <container_id>
To follow the logs: docker logs -f <container_id>

Docker exec
To go insider a container: docker exec -it <container_id|container_name> <command>

You might also like