How to Send Local Computer Files to a Docker Container

How to Send Local Computer Files to a Docker Container

Overview

This article will show the steps on how to send files from a local computer directory to a Docker container.

As an example, this tutorial will show how to make a Dockerfile that will create an Ubuntu image and install Python, to make a Docker container that will run a Python file. Other images can be used instead to run other files or programs if the requirements are stated in the Dockerfile. To see a list of available images on Dockerhub, visit their official website (1).

Materials

  • Computer

  • Docker

  • Terminal, Command Prompt, Powershell or IDE

  • Python

Procedure

  • Create the Dockerfile. Every Dockerfile's first line must start with "FROM". "FROM" specifies what image is being pulled. In this case, specifying "FROM ubuntu:latest" states the official Ubuntu image will be pulled. An image that is already available on Dockerhub can be pulled and run without a Dockerfile. The advantage of using a Dockerfile is also installing additional packages to customize the image.

  • The next few lines in the Dockerfile has "RUN....". Using the keyword "RUN" executes commands in order to create and install additional packages onto the image. Ubuntu uses apt-get as its package manager to download packages. The Ubuntu operating system is first updated and upgraded and afterward, the nano, python3, and pip packages are downloaded. After making the Dockerfile, save it as "Dockerfile". This file has no extension either.

Dockerfile

# Run Ubuntu
FROM ubuntu:latest

# Update and Upgrade Packages
RUN apt-get -y update
RUN apt-get -y upgrade

# Install nano, python, pip
RUN apt-get install -y nano
RUN apt-get install -y python-is-python3
RUN apt-get install -y python3-pip
  • After making the Docker container, go into the directory that has this Dockerfile. Now use the Terminal or a shell program to execute Docker commands to run the Dockerfile to make the Docker container.
docker build -t image_name .
docker run -it --name=container_name image_name /bin/bash
  • image_name and container_name are placeholders. Any name can be given to an image or container. If a container_name is not specified, docker gives a random name to the newly created container.

  • After executing build and run commands, entering the container successfully should appear like this:

  • To leave the container, type "exit". Using "exit" also stops the container from running. To enter back into the container, first, start the container if it's not running and then execute into the container again. Below are the docker start and exec ("exec" means execute) commands to enter an existing container:
docker start container_name
docker exec -it container_name /bin/bash

Sending the File

Below is the contents of the Python file:

print("This file contains only this sentence.")

To send the file to a container, the copy or cp command is used:

docker cp file_name containerid:/file_name
docker cp /directory/.../file_name containerid:/directory/.../file_name

The file_name must be specified and the directory if you aren't in the same directory as the file when performing this command. Next, provide the containerid of the newly created container. The containerid can be found performing "docker container ls" or "docker ps".

docker container ls

ls means list showing the entire list of containers

docker ps

ps means process status, showing the list of containers and which ones are running or not

docker cp file_name containerid:/file_name
docker cp /directory/.../file_name containerid:/directory/.../file_name

After specifying the containerid, ":/" must be written, and then write out the file_name. As an option, an existing directory can be specified to copy the file in that location. After performing the command, the copied file can be found in the container. Below are examples of how to perform the copy command:

A directory named "folder" was created before executing the second copy command. To make a new directory in the Ubuntu container use the "mkdir" command and specify a name for the directory:

mkdir "directory_name"

To check and run the Python files, use the dir and python commands respectively:

Sources

https://hub.docker.com/search?image_filter=official&q= (1)