Updated: 24.10.2022

Important commands:

  docker pull image-name
  docker run local-name image-name
  docker ps

  docker build

Use image

Some basic commands to start using Docker. Two things are important ie. Docker Image and Docker Container. To fetch RStudio image from Docker Hub run the command:

  docker pull rocker/rstudio

When the image has been pulled to the local Docker engine, you could create and run Docker container based on the pulled image with

  docker run --rm rocker/rstudio

Here the option --rm is used to tell docker to delete the container when exiting. Running docker run is actually a short command to docker create and docker start of Docker container. Other options that often are used when using docker run command include:

To expose port 8787 both for server inside the container and externally of image rocker/rstudio

  docker run -p 8787:8787 --name rstudio rocker/rtudio

To access RStudio from the webbrowser run http://localhost:8787

To check all pulled images in the local machine, run docker images command. It is wise to delete containers and images that aren’t used to reduce space. Deleting the images is done using docker rmi image-name command while deleting container if option --rm when running the container is docker rm container-name.

To list all containers, use either of these commands.

  docker container ls -a
  docker ps -a

The options -a will also list the containers with existed status. To delete all containers that are already existed, use the command docker container prune. Else, ordinary bash method defining all the options as shown in the second command line below:

  docker rm $(docker ps -a -q -f status=existed)

Without defining the specific options will only remove a certain container ie. docker rm rstudio. The option -q for just returning the numeric ID and -f for filtering output based on conditions provided.

Use docker stop to end container and docker restart to restart it.

Create Image

When no available image suits your need, than you can create one using Dockerfile which is a text file consists of commands that Docker client will call while creating an image. Example of Dockerfile copied from tutorial.

  FROM python:3

  # set a directory for the app
  WORKDIR /usr/src/app

  # copy all the files to the container
  COPY . .

  # install dependencies
  RUN pip install --no-cache-dir -r requirements.txt

  # define the port number the container should expose
  EXPOSE 5000

  # run the command
  CMD ["python", "./app.py"]

Then run docker build command from terminal as follows:

  docker build -t yourusername/catnip .

The option -t takes an optional tag name and a location of the directory containing the Dockerfile. Yourusername is the username from your Docker Hub account. When you will run the completed image, then run:

  docker run -p 8888:5000 yourusername/catnip

The command we just ran used port 5000 for the server inside the container and exposed this externally on port 8888. Head over to the URL with port 8888 with https://localhost:8888/

Mount directory

If you want to make local directory available in your container, the basic structure is as follow:

  docker run -it --name <WHATEVER> -p <LOCAL_PORT>:<CONTAINER_PORT> -v <LOCAL_PATH>:<CONTAINER_PATH> -d <IMAGE>:<TAG>

To implement then run:

  docker run -v myfolder:/data/myfolder image-name
  docker run --rm -it -v /mnt/F:/F silex/emacs

For multiple directory can be done with

  docker run -it --rm \
         -v '/mnt/F:/F' \
         -v '/mnt/F:/N' \
         silex/emacs

This has reference from StackOverflow.

Auto create Dockerfile

This blog nicely guides us on how to create Dockerfile for Shinyapp automatically using renv and friends.

Automating Dockerfile creation or directly to source

Docker 
comments powered by Disqus