xiaolingxiao
xiaolingxiao

Reputation: 4895

Mounting a local directory to remote docker container

I have a remote docker container which I pulled and is currently running using:

docker pull bamos/openface
docker run -p 9000:9000 -p 8000:8000 -t -i bamos/openface /bin/bash

I also have a local directory at

/Users/name/Documents/my-project

I need to import openface from scripts inside my-project while developing in ipython, but I do not know how to mount my-project onto openface, or should it be the other way around.

In general, I have been reading the docs here https://docs.docker.com/engine/tutorials/dockervolumes/#mount-a-host-directory-as-a-data-volume, but am still very confused. I would greatly appreciated it if someone could

  1. give the exact commands so that I can import openface from my-project
  2. explain aspects of the command so I am less confused :D

Thank you!

============================================================== EDIT:

right now I am doing:

docker run -v /Users/name/Documents/my-project:/root/my-project -p 9000:9000 -p 8000:8000 -t -i bamos/openface /bin/bash

and it seems to be working.

Upvotes: 1

Views: 3233

Answers (1)

Rafael
Rafael

Reputation: 1875

So, I will just point some topics you can study and give thoughts that I think can be useful for you.

As you are still learning, let's go through these topics:

  • Using a docker image
  • Custom a docker image
  • Integrate some docker images

Using a docker image

This is what you already know. Run docker run -v /Users/name/Documents/my-project:/root/my-project -p 9000:9000 -p 8000:8000 -t -i bamos/openface /bin/bash and start using it. Run some commands and see the capabilities of the image provided.

Use cases (for me):

  • Once, I needed test NoSQL Cassandra. I easily run docker run --name some-cassandra -d cassandra:latest and it is already working
  • I will make an Python/Flask app, so instead of making virtualenv, I prefer Docker.

Not so good for now, but it is interesting to use.

Custom a docker image

Instead of using ephemeral images, you can create your own. So, you need to learn about Dockerfile. Here is a simple example:

FROM bamos/openface

RUN pip install flask
COPY myentrypoint.sh

CMD ["myentrypoint.sh"]

Fast explanation:

  • Start your own image from bamos/openface
  • Run a command that install python package flask
  • Copy your entrypoint (you need to create one)
  • Run this entrypoint when user use your container

With this image, just save with name Dockerfile and run docker build -t chibro2/myopenface ., and then you have your own image with an extra package.

entrypoint is a default sh command that will start something (for example, a web server). If you have an online instance (AWS, for example) you can run your docker, and it will start your web service. If you tested locally, you should have the same results online. One common problem is testing with different versions (like python3 local, and python2 at the server).

Use cases:

  • Make versions of your infrastructure (you have at your git repository all packages required)
  • Make an image that will work easily on your server.

Check: https://www.digitalocean.com/community/tutorials/docker-explained-using-dockerfiles-to-automate-building-of-images https://docs.docker.com/engine/reference/builder/

Integrate some docker images

One thing that I really like at Docker, is the docker compose. You easily work with several docker images that work with each other. An example of docker-compose:

version: '3'

services:
  db:
    image: postgres
  web:
    build: .
    command: python3 manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

Fast explanation:

  • We create a container db, a postgres database
  • We create a webservice, build from Dockerfile (it is implicit in build), and run a server with command python3 manage.py runserver 0.0.0.0:8000.

Ps.: Yes, it use the code as a volume, but this something I don't recommend for production, but recommend for development (so, you easily change your code, and don't need to build the image).

With docker-compose, we easily integrate several projects and start all with a single command: docker-compose up

Use cases:

  • This is excelent for microservices
  • Services discovery is easy
  • Simple command for scale (docker-compose scale web=2 will create 2 instances of your web server).

Check this quickstart: https://docs.docker.com/compose/django/

Conclusion

There is too much to learn about docker. I can't give a very good answer on this, but I hope I could help a little.

Possible future steps is to use Docker Machine and Docker Swarm.

Upvotes: 1

Related Questions