Hi there! I hope this article can be a good reference to what I would consider the most important Docker commands :)
Command line reference
There are more than 50 docker commands, many of which you will likely never use. The table below presents the commands I have been using when developing and deploying my Docker project.
- docker images
- (lists all images)
- docker ps
- (list running containers)
- docker ps -a
- (list all containers)
- docker rm CONTAINERID
- (remove container with given CONTAINERID)
- docker rmi IMAGENAME:TAGNAME
- (remove image with given IMAGENAME:TAGNAME)
- docker commit CONTAINERID IMAGENAME:TAGNAME
- (save container to image)
- docker login
- (login to dockerhub)
- docker push USERNAME/IMAGENAME:TAGNAME
- (push the image to your dockerhub)
- docker pull USERNAME/IMAGENAME:TAGNAME
- (pull an image from dockerhub (not always necessary to provide USERNAME and TAGNAME))
- docker run XXX
- (this command will be described in detail below)
As seen in the command line reference section, the golang image can be downloaded using:
docker pull golang
(executing the docker run IMAGENAME will also download the image if it hasn’t been downloaded already)
I use Linux (Ubuntu) for my development. While working with Docker there are three
docker run parameters I always use:
-v /home/ME/docker/PROJECTNAME:/go/src golang:
docker run --publish 6060:8080 -it -v /home/ME/docker/MYPROJECT:/go/src golang
--publish 6060:8080parameter binds port 8080 of the container to port 6060 on 127.0.0.1 of the host machine.
-itparameter allocate a tty (terminal) to the container process, so we can easily build/run the project and download necessary tools.
-v /home/ME/docker/PROJECTNAME:/go/srcparameter mounts the /home/ME/docker/PROJECTNAME directory into the /go/src container directory.
This means that I have full access, through the terminal, to a linux environment configured to run Go. Moreover, I can easily create and modify files located in the containers /go/src directory, allowing me to use the Sublime text editor instead of vi/nano. Finally, when I run a web project on the container, then I can access it with my browser through localhost:6060.
This workflow is incredible for many reasons:
- the development and deployment environment is exactly the same (many big projects rely on outside dependencies, such as ffmpeg, imagemagick, youtube-dl, etc., and it can often be a pain to download and setup the right environment, not to mention you may have forgotten where/how you installed a given tool when it’s time to deploy it)
- if you installed unnecessary dependencies or fucked something up, then you can just throw the container away and create a new one
- everything is easily backed up in the cloud
- you rarely make changes to the container image itself, and you can easily git pull/push (both inside and outside the container if you mount it to a directory on the host computer)
- it doesn’t matter what operating system you/your team uses
- you can copy/paste between the container and host computer through the allocated terminal
- if the software you use (e.g. golang) is updated and you would like to use the latest version, then you can usually obtain it by simply pulling the Docker image
I mostly just commit and push code to a GitHub repo, and only commit/push to DockerHub if I downloaded something to the container.