How to convert docker command to docker-compose - docker

I wanted to convert docker run command to docker compose. Can you plz give some clue.
docker run -dit -h nginx --name=nginx --net=internal -p 8085:80 --restart=always -v /default.conf:/etc/nginx/conf.d/default.conf nginx:latest

Use docker run --help to understand what each of the used options does. Then proceed to the Compose file reference and find there how it is configured with YAML.
Note that some command line arguments have no equivalents in compose. That is either because they are not yet implemented or because they are also used as command line options. An example of the latter is -d, which run the container in detached mode. Its equivalent for docker-compose is also -d (e.g docker-compose up -d).

Related

docker run - autokill container already in use?

I was following this guide on customizing MySQL databases in Docker, and ran this command multiple times after making tweaks to the mounted sql files:
docker run -d -p 3306:3306 --name my-mysql -v /Users/pneedham/dev/docker-testing/sql-scripts:/docker-entrypoint-initdb.d/ -e MYSQL_ROOT_PASSWORD=supersecret -e MYSQL_DATABASE=company mysql
On all subsequent executions of that command, I would see an error like this:
docker: Error response from daemon: Conflict. The container name "/my-mysql" is already in use by container "9dc103de93b7ad0166bb359645c12d49e0aa4a3f2330b5980e455cec24843663". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.
What I'd like to know is whether that docker run command can be modified to auto-kill the previous container (if it exists)? Or if there is a different command that has the same desired result.
If I were to create a shell script to do that for me, I'd first run docker ps -aqf "name=mysql" and if there is any output, use that resulting container ID by running docker rm -f $containerID. And then run the original command.
docker run command has a --rm arguments that deletes the container after the run is completed. see the docs . So, just change your command to
docker run --rm -d -p 3306:3306 --name my-mysql -v /Users/pneedham/dev/docker-testing/sql-scripts:/docker-entrypoint-initdb.d/ -e MYSQL_ROOT_PASSWORD=supersecret -e MYSQL_DATABASE=company mysql

Pycharm docker run configuration do not accept environment variables

I am trying to set up a docker run configuration in Pycharm, i am pretty new to this functionality in pycharm, and i can't get it working.
In docker I would run the container with the following command
docker build -t test-container . && docker run --name container-pycharm -t -i --env-file .env -v $(pwd):/srv/app -p 8080:8080 --rm test-container ./serve-app
I set up this in pycharm, by adding the following line
--rm --env-file .env -i -t -p 8080:8080 -v $(pwd):/srv/app
to command line options section in the relevant docker Run/Debug Configuration Pycharm window. Unfortunately I get
Failed to deploy 'container-pycharm Dockerfile: Dockerfile': com.github.dockerjava.api.exception.BadRequestException: {"message":"create $(pwd): \"$(pwd)\" includes invalid characters for a local volume name, only \"[a-zA-Z0-9][a-zA-Z0-9_.-]\" are allowed. If you intended to pass a host directory, use absolute path"}
Clearly, I cant use $(pwd) in my command line options, any idea on how to solve this in pycharm?
Pycharm doesn't invoke docker directly via the command you see in the command preview, it goes through its custom parser, currently they haven't implemented the feature to read envs. Thus "If you intended to pass a host directory, use absolute path"
And -v is not officially supported as command line options in the current version. Ref
Use Bind mounts instead

How to get `docker run` full arguments?

For example, I run a docker by docker run -d --name sonarqube -p 19000:9000 -p 19002:9002 -e SONARQUBE_JDBC_USERNAME=sonar -e SONARQUBE_JDBC_PASSWORD=123 --link sonarqube-mysql:mysql.
Then I lost my shell command history, but I want to know all my arguments.
How can I get them? (I need the arguments to copy/move/restart container)
Of course docker inspect is the way to go, but if you just want to "reconstruct" the docker run command, you have
https://github.com/nexdrew/rekcod
it says
Reverse engineer a docker run command from an existing container (via docker inspect).
docker inspect CONTAINER_NAME gives you that information.
Check docker inspect reference to see all available options: https://docs.docker.com/engine/reference/commandline/inspect/

transform command line docker to Dockerfile

I have this dockers commands
1 - docker run -itp 3000:3000 --expose 3000 --name ead-courses-service
-v /home/dizie/Projects/node/ead-project-api/ead-courses:/home/ead-courses
-w /home/ead-courses --link mongodb node-service npm start
2 - docker run -itp 3001:3001 --name ead-proofs-service -v
/home/dizie/Projects/node/ead-project-api/ead-proofs:/home/ead-proofs
-w /home/ead-proofs --link ead-courses-service,mongodb node-service npm start
3 - docker run -itp 3002:3002 --name ead-students-service -v
/home/dizie/Projects/node/ead-project-api/ead-students:/home/ead-students
-w /home/ead-students --link mongodb node-service npm start
I like to execute with mode easier.
Is possible?
Example, Dockerfile or docker-compose.
Not sure if this help. If you are using Windows then just create a .bat file and put all your commands in it. You just have execute this .bat file and all your command will get executed.
If you are using unix/linux OS then create a .sh file and put all your commands in it.
As these are plain commands these should help.
A first approach just to get you started woudl be to:
run those images (you get three containers)
docker commit those containers (you get three images representing what was running)
apply to those images CenturyLinkLabs/dockerfile-from-image, which will generate a Dockerfile per image.

Must I provide a command when running a docker container?

I'd like to install mysql server on a centos:6.6 container.
However, when I run docker run --name myDB -e MYSQL_ROOT_PASSWORD=my-secret-pw -d centos:6.6, I got docker: Error response from daemon: No command specified. error.
Checking the document from docker run --help, I found that the COMMAND seems to be an optional argument when executing docker run. This is because [COMMAND] is placed inside a pair of square brackets.
$ docker run --help
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container
I also find out that the official repository of mysql doesn't specify a command when starting a MySQL container:
Starting a MySQL instance is simple:
$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
Why should I provide a command when running a centos:6.6 container, but not so when running a mysql container?
I'm guessing that maybe centos:6.6 is specially-configured so that the user must provide a command when running it.
if you use centos:6.6, you do need to provide a command when you issue "docker run" command.
The reason the officical repository of mysql does not specify a command is because it has CMD command in it's docker file: CMD ["mysqld"]. Check it's docker file here.
The CMD in docker file is the default command when run the container without a command.
You can read here to better understand what you can use in a docker file.
In your case, you can
Start your centos 6.6 container
Take official mysql docker file as reference, issue similar command (change apt-get to yum ( or sudo yum if you don't use the default root user)
Once you can successfully start mysql, you can put all your command in your docker file, just to make sure the first line is "From centos:6.6"
Build your image
Run a container with your image, then you don't need to provide a command in docker run
You can share your docker file in docker hub, so that other people can user yours.
good luck.

Resources