-
How to use Docker in Pycharm - 1 - Configure Container and Save it as an ImageVirtual Environment (Docker) 2021. 1. 20. 04:00
How to use Docker in Pycharm
I prefer terminal commands than GUI but when I use Pycharm… I had to know how to configure things with GUI.
This post will show you how to
- Start a Docker Container with the Docker Image
- Configuration of a Docker Container
- Save the Container as an Docker Image
OS: Linux, Debian, Ubuntu 16.04
1. Start a Docker Container with the Docker Image
ref: learnopencv.com/install-opencv-docker-image-ubuntu-macos-windows/
I was trying to use OpenCV at the moment so let’s start with this page and I will assume you already have your Docker server running and you already have your Docker Image you want to use.In the
8:Services
tab, I already added Docker service in Pycharm and named the daemon asVideoAnalysis
.
You can see thespmallick/opencv-docker:opencv
docker image under the Images.2. Configuration of a Docker Container
If you right click the name of the image, you can see Create Container, and if you click this Pycharm will show you a Docker Configuration editor.
So the Docker running command, can be configured in this editor.
For example, if I want to run the command below,docker run -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=$DISPLAY -p 5000:5000 -p 8888:8888 -it spmallick/opencv-docker:opencv /bin/bash
This command can be saved as a configuration in Pycharm like the image below. *I also added mounting point with
Bind mounts
menu, so that I can use my scripts in it without saving it in the image.Here, I named the docker container as
video_analysis
. *--device=/dev/video0:/dev/video0
tag will not work if you don’t have the camera.
Now, you can apply this settings and run the container.
The configuration editor can be found if you right click the name of the container.
Multiple configurations for each image can be saved.3. Save the Container as an Docker Image
So here I wanted to change the image so that I don’t have to run trivial stuffs. You remember I put
/bin/bash
command to be executed in the configuration.
This one means sourcing~/.bashrc
in the container.
Then I added some commands I would like to execute by default.
In this case, I was going to use OpenCV-3-4-3 and it was configured in an virtual environment of the docker image. (I don’t understand why people use virtual environments in the virtual environments but… anyway…)
So, I added a command lineworkon OpenCV-3.4.3-py3
into~/.bashrc
file like below.echo workon OpenCV-3.4.3-py3 >> ~/.bashrc
Then I want to save this container as an image and create a new container with the new image. The reference page shows you how to make a new image from the container more in detail but I will show you what I’ve done as well.
ref: www.scalyr.com/blog/create-docker-image/All we need is the Container ID or the name of the Container name. I will just use the Container name.
I was trying to find how I can do this with GUI but I couldn’t so I just used terminal to run this command below.docker commit video_analysis_test new_opencv_image
new_opencv_image
is the tag of the image.
Now you can see the new image is added in the Docker server.
Finally, you can create a new container with this new image.To be continued…
Next, I will show you how to use the virtual environment in the docker image as an interpreter of Pycharm.
Written with StackEdit.
'Virtual Environment (Docker)' 카테고리의 다른 글
How to use Docker in Pycharm - 2 - Python Interpreter (0) 2021.01.20