Docker quick commands
Get Started
For more informations access the official documentation.
Installation
Test Docker version
Run docker --version
and ensure that you have a supported version of Docker:
$ docker --version
Docker version 18.03.1-ce, build 9ee9f40
Test Docker installation
$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete
Digest: sha256:ca0eeb6fb05351dfc8759c20733c91def84cb8007aa89a5bf606bc8b315b9fc7
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
...
Commands
Create a new container
$ docker run ubuntu
List active containers
$ docker ps
List all containers
$ docker ps -a
-q, --quiet
Only display numeric IDs $ docker ps -q
Create a new container and active interative terminal
$ docker run -it ubuntu
For exit the terminal press Ctrl
+ d
.
Start an container
$ docker start CONTAINER_ID
Start an container with interative terminal
$ docker start -a -i CONTAINER_ID
Stop an container
$ docker stop CONTAINER_ID
Seconds to wait for stop before killing it (default 10) -t, --time int
$ docker stop -t 0 CONTAINER_ID
Stop all active container
$ docker stop -t 0 $(docker ps -q)
Remove container
$ docker rm CONTAINER_ID
Remove all inactive containers
$ docker container prune
List all images
$ docker images
Remove image
$ docker rmi REPOSITORY_NAME
Remove all images
$ docker rmi $(docker images -q)
--detach option
Run container in background and print container ID docker run -d dockersamples/static-site
-P, --publish-all
Publish all exposed ports to random ports $ docker run -d -P dockersamples/static-site
$ docker port CONTAINER_ID
443/tcp -> 0.0.0.0:32768
80/tcp -> 0.0.0.0:32769
Note: 0.0.0.0:32768
= localhost:32768
.
-p, --publish list
Publish a container's port(s) to the host # 8080:80 => MY_LOCAL_PORT:DOCKER_CONTAINER_PORT
$ docker run -d -p 8080:80 --name site-sample dockersamples/static-site
$ docker port 08e0b419d760
80/tcp -> 0.0.0.0:8080
Note: 8080
port my machine and 80
port of container.
--name string
.
Assign a name to the container $ docker run -d -P --name site-sample dockersamples/static-site
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5c86c9424798 dockersamples/static-site "/bin/sh -c 'cd /usr…" About a minute ago Up About a minute 0.0.0.0:32771->80/tcp, 0.0.0.0:32770->443/tcp site-sample
-e, --env list
Set environment variables $ docker run -d -p 8080:80 -e AUTHOR="Eder Sampaio" dockersamples/static-site
Open http://localhost:8080
in the browser:
-v, --volume list
Bind mount a volume $ docker run -it -v "/Users/edersampaio/docker-sample:/var/www" ubuntu
Keeps folders and files created in /var/www
, same after remove container.
Run container and modify files locally
$ docker run -it -p 3000:3000 -v "/Users/edersampaio/docker-node-sample:/var/www" -w "/var/www" node:9.11.1-alpine npm start
# or
$ cd /Users/edersampaio/docker-node-sample
$ docker run -it -p 3000:3000 -v "$(pwd):/var/www" -w "/var/www" node:9.11.1-alpine npm start
Dockerfile
Create a Dockerfile
# image
FROM node:9.11.1-alpine
# maintainer
LABEL maintainer="edersampaio@outlook.com.br"
# copy files
COPY . /var/www
# working directory
WORKDIR /var/www
# install node dependencies and start server
RUN npm install
ENTRYPOINT [ "npm", "start" ]
# export port
EXPOSE 3000
Dockerfile with Node Enviroments
# image
FROM node:9.11.1-alpine
# maintainer
LABEL maintainer="edersampaio@outlook.com.br"
# node envs
ENV NODE_ENV=production
ENV PORT=3000
# copy files
COPY . /var/www
# working directory
WORKDIR /var/www
# install node dependencies and start server
RUN npm install
ENTRYPOINT [ "npm", "start" ]
# export port
EXPOSE $PORT
Create image
$ docker build -f Dockerfile -t ederssouza/node-alpine .
# or
$ docker build -t ederssouza/node-alpine .
Run container
$ docker run -d -p 8080:3000 ederssouza/node-alpine
Docker Hub
Publishing your image on Docker Hub
# entry with your login and password
$ docker login
Login Succeeded
$ docker push ederssouza/node-alpine
Downloading image of Docker Hub
$ docker pull ederssouza/node-alpine