Commands

List all the images

docker images

Download image

https://hub.docker.com/

# docker pull [image]
docker pull mysql

Delete image

# docker image rm [image]
# docker image rm [image]:[tag]
docker image rm mysql
docker image rm mysql:latest
docker image rm mysql:16

Create a container with the image

With random container name

It will generate an ID to the container.

# docker container create [image]
# docker create [image]
docker create mysql

Id generated:

a6e16354239fc76b4cc89216bfe46f0260500a7e798d62e319af213345bb13c8

With custom container name

docker create --name monguito mongo

Id generated:

e1d70ca8719dcf07987eae76bac9a621d4531e9c9b8bdf8328b647e649d3f438

Start a container

Using the ID of the container

# docker start [container_id]
docker start a6e16354239fc76b4cc89216bfe46f0260500a7e798d62e319af213345bb13c8

Using the custom name of the container

docker start monguito
docker ps # we will see the container running

Using environment variables

For example, to create a mongodb container with specific user and password, to then use an app to connect to it, we use the env variable described in mongodb hub:

https://hub.docker.com/_/mongo

Each image has different ways to configure

docker create -p27017:27017 --name monguito -e MONGO_INITDB_ROOT_USERNAME=nico -e MONGO_INITDB_ROOT_PASSWORD=password mongo

Status of containers running

docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS       NAMES
a6e16354239f   mysql     "docker-entrypoint.sā€¦"   4 minutes ago   Up 2 seconds   27017/tcp   pedantic_rubin

Stop a container

docker stop a6e16354239f
docker ps
# There is no one container running

List of containers started and off

List all the containers in system.

docker ps --all
docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED        STATUS                    PORTS     NAMES
a6e16354239f   mysql     "docker-entrypoint.sā€¦"   22 hours ago   Exited (0) 22 hours ago             pedantic_rubin

Delete a container

Using name

docker rm pedantic_rubin
docker ps -a #container deleted

Expose a container in a host port

Custom port

# docker create -p[port_in_host]:[port_in_container] --name [name] [image]
docker create -p27017:27017 --name monguito mongo
docker start monguito
docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                                           NAMES
6c97203a3708   mongo     "docker-entrypoint.sā€¦"   2 minutes ago   Up 2 seconds   0.0.0.0:27017->27017/tcp, :::27017->27017/tcp   monguito

Random port

# docker create -p[port_in_container] --name [name] [image]
docker create -p27017 --name manguito mongo
docker start manguito
docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                                           NAMES
2bdec62e384c   mongo     "docker-entrypoint.sā€¦"   6 seconds ago   Up 1 second    0.0.0.0:32768->27017/tcp, :::32768->27017/tcp   manguito

Check logs of container

docker logs monguito

Or to stay watching those logs:

docker logs --follow monguito

Download the image, create and start the container

With logs view, random data

docker run mongo

Without logs view, detach mode and random data

docker run -d mongo
Unable to find image 'mongo:latest' locally
latest: Pulling from library/mongo
57c139bbda7e: Pull complete 
2a7c884ecb1c: Pull complete 
ddcbc3e219f6: Pull complete 
347abad2978b: Pull complete 
33ad7d6a71b8: Pull complete 
46b764746687: Pull complete 
0d22e1cb5ef2: Pull complete 
fef1dc21c099: Pull complete 
Digest: sha256:175ee5c400f24d2766cea492b4986851b4e7ec75fc08e354673149b86488019c
Status: Downloaded newer image for mongo:latest
309059638da6dc66af6cd40312bef60c36b1b7b6056bc6b3f38d824ab39b2e26

It generates our container ID:

309059638da6dc66af6cd40312bef60c36b1b7b6056bc6b3f38d824ab39b2e26
docker ps          
CONTAINER ID   IMAGE     COMMAND                  CREATED              STATUS              PORTS       NAMES
309059638da6   mongo     "docker-entrypoint.sā€¦"   About a minute ago   Up About a minute   27017/tcp   crazy_thompson

Without logs view and custom data

docker run -d --name monguito -p27017:27017 mongo
#a146ef22b6bb96889a273d8037f4613eefb5519e063faa8f84555e1fb43072e8
docker ps                                        
CONTAINER ID   IMAGE     COMMAND                  CREATED              STATUS              PORTS                                           NAMES
a146ef22b6bb   mongo     "docker-entrypoint.sā€¦"   About a minute ago   Up About a minute   0.0.0.0:27017->27017/tcp, :::27017->27017/tcp   monguito

Interact with networks

List networks

docker network ls
NETWORK ID     NAME      DRIVER    SCOPE
b17449f934b1   bridge    bridge    local
6b89bd5bc050   host      host      local
fdc75e667bd0   none      null      local

Create a network

# docker network create [nombre]
docker network create mired
NETWORK ID     NAME      DRIVER    SCOPE
b17449f934b1   bridge    bridge    local
6b89bd5bc050   host      host      local
9cbcc9aa1f95   mired     bridge    local
fdc75e667bd0   none      null      local

Delete a network

docker network rm mired

Last updated