-
Notifications
You must be signed in to change notification settings - Fork 1
Docker Quick Start Guide
Install docker
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo apt-key fingerprint 0EBFCD88
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get -y install docker-ce
Install docker-compose
sudo curl -L https://github.com/docker/compose/releases/download/1.18.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
Check Installation
- check your installation by typing the following commads
docker
docker-compose
## List Docker CLI commands
docker
docker container --help
## Display Docker version and info
docker --version
docker version
docker info
## Execute Docker image
docker run hello-world
## List Docker images
docker image ls
docker images
## List Docker containers (running, all, all in quiet mode)
docker container ls # or docker ps
docker container ls --all
docker container ls -aq
## Get Docker Images from DockerHub
docker pull image_name:latest
## Tag a Docker Image
docker tag my_image:latest renamed_image:new_tag
Docker Image
Contains the structure of your application with all needed resources to run. Technically images themselves cannot be run and have to be instantiated as containers but you will often read in this guide about “running the image”, as it is a common short way of referring to this kind of instantiation. Images are created from a Dockerfile with the docker build command. Images are stored in a Docker registry, such as DockerHub and can be downloaded with the docker pull command.
Docker Container
Is an instance of an image which starts working when image is called with docker run command.
You can list running containers with docker ps
.
docker run \
-e GRANT_SUDO=yes # sets env variable GRANT_SUDO to YES \
-v /path/where/bound/dir/is:/path/inside/container/ \
# create a 'bind mount' instead of a volume
# volume == visible only to Docker
# bind mount == image/container can be placed
# anywhere in a host filesystem
--name name-of-container \
-it # emulate an interactive pseudo-terminal \
--rm \ # automatically remove the container upon exit
-p 4000:80 \ # redirects to localhost:4000 port 80 of the
# docker container
user/name-of-container:latest
After this runs, you can go to localhost:4000
to see the output.
Thanks to the -it
option you can access the container through an emulated terminal:
docker exec -it name-of-container bash
# or
docker exec -t -i name-of-container /bin/bash
Detached mode lets you run your image in the background
docker run -d —-name name-of-container user/name-of-container:latest
Containers can be also just created and started later. Instead of docker run you use docker create
and docker start
commands to create and start the container respectively.
docker create -P --expose=8090 user/name-of-container:latest
docker ps -a # option -a shows also the stopped containers
docker start afd23221afd2 # use hash of the container to start it in detached mode
To stop a running container one can use either docker stop
or docker kill
command.
docker stop afd23221afd2 # sends a SIGTERM and after some time (grace period)
# if there is no response a SIGKILL
docker kill afd23221afd2 # sends a SIGKILL
This only stops container from being executed, you can use docker restart afd23221afd2
to spin it back up. If you run docker ps -a
you will still see the container. You can remove the container with the docker rm
command:
docker rm afd23221afd2 # removes a container
But this does not remove any images. If for some reason you don’t need the image anymore use docker images ls
command to list images and copy the hash of the image you want to delete. Then run
docker rmi 49bc9087df34 # given 49bc9087df34 is the hash of the image
docker stop $(docker ps -q)
docker rm -v $(docker ps -aq)