Skip to content

Latest commit

 

History

History
84 lines (69 loc) · 3.31 KB

LAB06-DockerTransferringContent.md

File metadata and controls

84 lines (69 loc) · 3.31 KB

LAB-06: Transferring Content between Host PC and Docker Container

This scenario shows how to transfer content between host and container:

  • Copying content from host PC to container
    • Using image file (COPY command)
    • Using bind mount/volume
    • Using cp command into the running container
  • Copying content from container to host PC
    • Using cp command from the running container
  • Copying content from existed Docker Volume object to host PC
  • Copying content from existed Docker Volume object to remote PC

Copying content from host PC to container

Using image file (COPY command)

FROM python:alpine3.7
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
EXPOSE 5000
CMD python ./index.py

Using bind mount/volume

  • Binding mount and volume: e.g. DockerVolume.md
  • Using '-v [volumeName]:[containerDirectoryPath]' or '-v [hostDirectory]:[containerDirectoryPath]'
docker volume create [volumeName]
docker volume create test
docker container run --name mywebserver3 -d -p 80:80 -v test:/usr/share/nginx/html nginx
docker container run --name mywebserver -d -p 80:80 -v C:\Docker\Webpage:/usr/share/nginx/html nginx

Using cp command into the running container

docker container run --name mywebserver -d -p 80:80 nginx
docker cp "C:\Docker\Webpage" mywebserver:/Webpage
docker cp [hostPath] [containerName]:[containerPath]

image

Copying content from container to host PC

Using cp command from the running container

docker container run --name mywebserver -d -p 80:80 nginx
docker cp mywebserver:/usr/share/nginx/html "C:\Docker\Webpage2" 
docker cp [containerName]:[containerPath] [hostPath] 

Copying content from existed Docker Volume object to host PC

  • Create a temporary container that is connected to the volume
  • Copy the content from container to host PC
docker volume create test
docker container run --name mywebserver -d -p 80:80 -v test:/usr/share/nginx/html nginx
docker container create --name temp1 -v test:/temp busybox ('temp1': temporary busybox container which is binded to 'test' volume )
docker cp temp1:/temp "C:\Docker\Webpage3" (copying from temporary container to host PC)
docker cp [containerName]:[containerPath] [hostPath] 

image

Copying content from existed Docker Volume object to remote PC

  • Creatíng archive to standard output using smallest image 'busybox' (1.23MB)
  • SSH to target host and extract files in destination container volume
(CONTAINER=myproject_db_1 REMOTE_HOST=newhost DIR=/var/lib/mysql; \
    docker run --rm \
    --volumes-from "$(docker inspect --format={{.Id}} $CONTAINER)" \
    busybox tar -czv --to-stdout $DIR  \
    | ssh $REMOTE_HOST docker run --rm -i \
    --volumes-from "\$(docker inspect --format={{.Id}} $CONTAINER)"\
    busybox tar -xzv \
    )

Details: https://medium.com/@bmihelac/copy-docker-container-data-volume-to-another-host-931568bda7fa