Show Menu

Docker Basics Cheat Sheet by

docker

Docker Image

docker images
See all available images
docker build -t <na­me>
Create an image with a pretty name (you must define the Docke­rfile in the folder)
docker tag <na­me> userna­me/­rep­osi­tor­y:tag
This tags an image ready to be sent to a repository
docker push userna­me/­rep­osi­tor­y:tag
Push the image to the remote repository
docker search <ke­ywo­rd>
Search for public reposi­tories
Docker Images are the base for conta­iners and are similar to .iso files. They can be for example the image of your app and contain everything needed to run the applic­ation.

These images can be local or in reposi­tories (and marked with an tag)

To create images, you must create a Docke­rfile with some docker commands to specify how that image will be created, for example to setup the enviro­nment and a BaseImage.

Services

Different pieces of the app are called “services” For example, a service for storing applic­ation data in a database, a service for the front-end, etc.

Services are just “conta­iners in produc­tion.” A service only runs one image, but it manages for example what ports it should use and how many replicas of the container should run.

To define a service, you'll need an docke­r-c­omp­ose.yml file.

For example:
version: "­3"
servi­ces:
web:
image: amiche­let­ti/­get­-st­art­ed:­part1
deploy:
replicas: 5
resour­ces:
limits:
cpus: "­0.1­"
memory: 50M
restar­t_p­olicy:
condition: on-fai­lure
ports:
- "­80:­80"
networks:
- webnet
netwo­rks:
webnet:

Here you define the image to be loaded, how many replicas, the resource limits and the restart condit­ions.

To run it you must first start: docker swarm init

Then run it giving a name:
docker stack deploy -c docker­-co­mpo­se.yml <ap­p_n­ame­>

To see the details of containers running in your service, run:
docker stack ps <ap­p_n­ame­>

Now each time you request your app (via browser, for example), the load-b­alancer will help you distri­buting the requests to each replica.

To put it down, docker stack rm <ap­p_n­ame­>
This puts down the app, but not the "­one­-no­de" swarm we created. Use:
docker swarm leave --force
Docker Swarm is available only for version "­3"
 

Docker Container

docker run <im­age­>
Run the image, starting a Container
-d
Run in detached mode (in backgr­ound)
-p 4000:80
Maps the port 80 of the image to the host port 4000
--rm
Removes the container when exited
docker ps
List the running containers (you can check container id)
docker ps -l
List all the containers (you can check container id)
docker stop <co­nta­ine­r_i­d>
Stop the container
When you run an image with you are starting a Container, so container is the runtime instance of an image, and consists of the image, an execution enviro­nment and a standart set of instru­ctions.

Swarm

docker swarm init
Initialize a swarm and become swarm manager
docker swarm join
Join a swarm as worker
docker swarm leave --force
Leaves the current swarm
With Docker you can increase resource and capacities by creating a swarm, which are simply several machines (virtual or physical) running a Docker and joined to a cluster.

Swarms have the swarm manager, which can issue docker commands normally, and the workers which are only there to provide capacity.

Download the Docker Basics Cheat Sheet

2 Pages
//media.cheatography.com/storage/thumb/amicheletti_docker-basics.750.jpg

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!

 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          docker-compose Cheat Sheet

          More Cheat Sheets by amicheletti