Table of Contents
Introduction
This is a cheat sheet created to help me “remember” the core commands of Docker. So in essence it is not supposed to have any explanations, just a central place to copy code. Bellow you can get the references, so you can find from where it all was taken. If you need to better understand and get more information about Docker, like how to install and things like that, this is the place to go: https://docs.docker.com/
The Most Helpful Command
docker COMMAND --help # there is an universe here!!!!
A Dockerfile Example
# Use an official Python runtime as a parent image
FROM python:2.7-slim
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
Basic Commands List
Create image using this directory’s Dockerfile
docker build -t friendlyhello .
Run “friendlyname” mapping port 4000 to 80
docker run -p 4000:80 friendlyhello
Same thing, but in detached mode
docker run -d -p 4000:80 friendlyhello
List all running containers
docker container ls
List all containers, even those not running
docker container ls -a
Gracefully stop the specified container
docker container stop <hash>
Force shutdown of the specified container
docker container kill <hash>
Remove specified container from this machine
docker container rm <hash>
Remove all containers
docker container rm $(docker container ls -a -q)
List all images on this machine
docker image ls -a
Remove specified image from this machine
docker image rm <image id>
Remove all images from this machine
docker image rm $(docker image ls -a -q)
Push and Pull Docker Images to and from Docker Hub Repository
The Docker Hub repository is the “Github” for your images. To be able to use it, first create a Docker account here.
Log in this CLI session using your Docker credentials
docker login -u your_docker_username -p password
Tag <image> for upload to registry
docker tag <image> username/repository:tag
Upload tagged image to registry
docker push username/repository:tag
Run image from a registry
docker run username/repository:tag
Moving Images from Host to Host with Export and Save
Saves a container’s running or paused instance (assuming we are in one) to a file
docker export defc | gzip > ubuntu.tar.gz
Importing the exported image
gzcat ubuntu.tar.gz | docker import - ubuntu-alice
Moving Using save
docker save ubuntu | gzip > ubuntu-golden.tar.gz
Loading in another Host
gzcat ubuntu-golden.tar.gz | docker load
References
https://blog.giantswarm.io/moving-docker-container-images-around/
https://docs.docker.com/get-started/part2/#build-the-app
What do you think?