Skip to content

Instantly share code, notes, and snippets.

@yezil3
Last active April 13, 2024 04:46
Show Gist options
  • Save yezil3/f6e3fb989ec436299fdfa614cac4212c to your computer and use it in GitHub Desktop.
Save yezil3/f6e3fb989ec436299fdfa614cac4212c to your computer and use it in GitHub Desktop.
conda-enabled docker: building, publishing and using

Build a conda-enabled container:

add user account to the sudo group to avoid using sudo:

sudo gpasswd -a $USER docker

newgrp docker

sudo setfacl -m user:$user:rw /var/run/docker.sock

pull an anaconda image:

docker pull continuumio/anaconda3

create a container using this image :

docker run --name $container -idt continuumio/anaconda3

test if conda is works and find the location of conda:

conda info --envs

copy the environment file and the project code to the container:

conda env export > requirments.txt

docker cp /home/$user/anaconda3/envs/$envName $container:/opt/conda/envs

docker cp /home/$user/$project $container:/root/

check the container id from:

docker ps

(or directly copy it from the terminal)

name a version and commit the container:

docker commit -a '$author' -m 'conda available' $containerID $image:$version

check the image ID:

docker images

login before push and push the container to the dockerhub:

docker login

docker tag $imageID $dockerhubUser/$repo:$version

docker push $dockerhubUser/$repo:$image

Using the image:

pull the image to local:

docker pull $dockerhubUser/$repo:$image

run in the container:

docker run -it --rm --gpus all -v /home/$serverUser/:/home/$serverUser/ $dockerhubUser/$repo:$image

Some useful commands:

exit from docker container:

exit

delete the container after exit:

docker container run --rm $dockerhubUser/$repo:$image

not delete the container after exit/need to commit after exit:

docker container run -it $dockerhubUser/$repo:$image

check all the running containers:

docker ps

check all the containers including the deactivated ones:

docker ps -a

check all the images:

docker images

delete an image:

docker image rm -f $dockerhubUser/$repo:$image

logout dockerhub

docker logout

Addtional helpful commands

Freeze an environment and create the environment on the conda-envable container:

on the server:

conda list -e > requirements.txt

sed -e 's/=pypi_0//' requirements.txt > reqs.nopip.txt

run the container and create:

docker run -it --rm --gpus all -v /home/$serverUser/:/home/$serverUser/ $dockerhubUser/$repo:$image

conda create -n $envName --file reqs.nopip.txt

or:

conda env export > environment.yaml

docker run -it --rm --gpus all -v /home/$serverUser/:/home/$serverUser/ $dockerhubUser/$repo:$image

conda env create -n $envName -f environment.yaml

Reference: [1] https://www.cnblogs.com/zuti666/p/17274324.html. [2] https://www.ruanyifeng.com/blog/2018/02/docker-tutorial.html.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment