The goal of this project was to test the feasibility of running python projects in Docker containers to simplify running them outside of my local machine (I intend to move longer running jobs to my desktop this way) in the future.
- Install Docker
- Build the project image
- Run the project as a container
- Do a little dance
- Do it again with a Registry (optional: This was what caused me suffering, but it was also the reason I did this in the first place)
- Create a local Registry (as a container ofc)
- Push your image to the registry
- Delete your local copy
- Pull the image back from the registry
- Run the image as a container again
- This will be easiest with docker desktop on windows. If you're running linux you probably don't need my help to install stuff.
- Here is the link to download Docker Desktop, which is an application that contains docker engine (which is open source and all that jazz) and some other stuff on top to make docker a bit friendlier to those who aren't super familiar with a terminal. We will be using the terminal for most of this guide.
NOTE: You will need to make an account for docker desktop, but you can do it with your google account so it's fairly painless.
- Once you've installed Docker, open a terminal window and run:
docker run hello-world(may needsudoon linux \o/) to verify that Docker is installed and working.
- In a terminal window navigate to this folder wherever you've put it (open folder in vscode is a quick way to do this)
- Run
docker build -t python-test .-tsets the 'tag' of the image to "python-test".is the directory in which Docker should look forDockerfile
- In your terminal (from wherever now) run
docker run --name python-test-container python-test--namesets the name of the container created
- You should now see "Hello World!" in your terminal.
- Wanna see it again?
- run
docker ps -ato list all containers on your machine- you should see
python-teston there
- you should see
- run
docker start -a python-test-aruns the container "attatched" so it connects stdin/out to your terminal.
- Did it do it again!?
- run
- If you'd like to go one step further to see the fairly simple process that has caused me so much pain the last few days I will walk you through that as well but just the painless low effort part.
- If you'd like a slightly more detailed explaination you can open the .html file in the
RegistryWalkthroughfolder for the guide I followed but here are the commands:
-
Run
docker run -d -p 5000:5000 --restart unless-stopped --name registry registry:2in terminal-druns the container in detatched mode (no terminal)--restartspecifies how/if the container should be restarted by the daemon on exit.- The guide uses
alwaysbutunless-stoppedlets you manually stop it without deleting the whole container.
- The guide uses
-pspecifies publish container port to host port--namesets the container name to registry
-
Run
docker psto confirm the container is running -
Configure docker to use http for the registry (otherwise it will get upset that it's insecure)
- Add
{ "insecure-registries": ["your_hostname.local:5000"] }to yourdaemon.jsonfile.
- Linux: Open the
/etc/docker/daemon.jsonfile, if this file doesn't exist, just make it. - Windows: I found it easiest to open docker desktop and go to
Settings > Docker Engineand add it there if you don't want to dig through your drive.- DID YOU KNOW: Your hostname can be found in your system settings and if you're trying to use it instead of your ip address, you should add .local and it should do the trick (assuming you're on the same local network), because I sure didn't!
- A SECOND NOTE: it seems that my network is not case sensitive as I can ping my laptop @
smallpapa.localandSmallPapa.localno problem, but docker'sdaemon.jsondoes appear to be case sensitive. Be aware, as this could potentially cause an issue.
- Add
- To push to your registry first tag your image with the ip and port it is destined for:
docker tag python-test your_hostname.local:5000/python-test
- Then push the image:
docker push your_hostname.local:5000/python-test
- Run
docker imagesto list all the images on your machine. - Use
docker rmi your_hostname.local:5000/python-testto remove the corresponding image. - To be extra sure you can also run
docker rmi python-test -fto remove the non-registry version-fforces deletion
- Running
docker imagesagain should reveal that the images are all nice and gone.
- Now to undo all your nice work run
docker pull your_hostname.local:5000/python-testto bring back what you've lost. - Because I dislike typing I'll make us do more now, running
docker tag your_hostname.local:5000/python-test python-testnow you don't have to type the full registry address.
- Run
docker run --name python-test2 python-test- That should do it.
docker ps -ashould list all your containersdocker rm [container-name]should delete a target containerdocker imagesshould list your imagesdocker rmi [image-name/id]should delete an imagedocker container prunewill delete all stopped containersdocker image prunewill delete all images that are untagged and unreferenced by containers. runningdocker image prune -awill remove all containers that aren't referenced by a container.