This is my personal reference on how to code in Python using Docker.
Python is one of my top programming languages. These days, one thing I avoid is to install many things on my computer just to try that piece of code or just an idea; this applies to the programming languages I use these days (Ruby, Go and C#). Once you install the tools, change your laptop and try to reproduce your environment; that’s one of the main issues for me (I see you .NET).
Then you find Docker. You need to try it to understand the power it can bring to your skills and how it can increase productivity and make you smile again. How easy is to drop a sandbox and repeat your environment again. There is a funny post by Nick Janetkis but with a serious message for developers having same problem I mentioned.
Install Docker: Go here for installations. During the day I work on an Windows 7 laptop, in which, by using VirtualBox I set up an Xubuntu VM where I play with Docker. Pretty nice for my purposes.
Ok, time for the notes. House keeping:
Let’s check if docker is running. Run in terminal or git-bash the following command:
Note: if by some reason, you see an error as follows:
$ docker version
API version: 1.24
Go version: go1.6.4
Git commit: 78d1802
Built: Wed Jan 11 00:23:16 2017
An error occurred trying to connect: Get http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.24/version: open //./pipe/docker_engine: The system cannot find the file specified.
Then check this post.
Run the docker hello world example:
docker run hello-world
- Will check if hello-world image is cached in computer (otherwise it will download image from hub.docker.com)
- Then it will create a container
- Run the container.
- If you get a raw
Hello from Dockermessage, you are good to continue.
My VM has python 3.5.2 (I need an installed version if I plan to code in my VM):
For compatibility reasons I want a Docker image with same version ():
docker pull python:3.5.2
Navigate to hub.docker.com to search for an image that suit your needs. In our case, I searched for python and from the list I choose the version for 3.5.2.
Verify your container’s python version:
docker run python:3.5.2 python --version
This docker’s command instructs to run against an image python with version 3.5.2 the code
python --version. The expected result is a legend
In a similar way, I can run others commands against the new container.
docker run python:3.5.2 ls
NOTE: keep in mind this keeps adding new containers to the list. Find this by running:
docker ps -a
Top entries belong to your previous “docker run..” executions.
Another nice way to run docker is by running in a throwaway container:
docker run --rm python:3.5.2 sh -c \ "python --version"
docker run --rm python:3.5.2 sh -c \ "python --version && exec python --version"
--rmtell to Docker CLI to automatically issue a docker rm command once the container exists. That way, we don’t leave anything behind ourselves.
- We chain the built step (
python --version) and the execution steps (
exec python --version) using the shell logical operator
&&. So, in this line we run 2 commands on the shell inside the container.
- We pass our commands to
sh -cbecause if we were to simple
docker run python:3.5.2 "python --version", Docket would try to execute the program but it will fail. So instead, we start a shell and instruct the shell to execute the command sequence.
To create a dockerfile, at least should have something like following:
COPY . /src
RUN ["python", "--version"]
This Dockerfile instructs to use docker image python:3.5.2, then copy the content of the folder’s current content (if any content) into a “src” folder inside the image, finally run bash command python with parameter –version.
Finally, docker development tends to create a new image from solution we are creating. To do this:
docker build -t gpalazuelos/mydemo .
This docker command instructs to build a new image, with tag (name) gpalazuelos/mydemo, takes the code in current folder (.). To this action, some people refers to it as containerize an application.
This creates the container, executes the code and finalize the container. If you run command
docker ps -a
you won’t find this container.