docker: how to set port in dockerfile [duplicate] - bash

This question already has an answer here:
Bind container port to host inside Dockerfile
(1 answer)
Closed 3 years ago.
When I call the docker run command in my terminal. The server starts up fine and is accessible, but when I try to add the port in the dockerfile. It does not work.
Is there a way I can set the port in the dockerfile explicitly? Thanks for any help.
This works:
docker run -d -p 5555:4444 -v /dv/sm:/dv/sm sa:latest
I remove the -p flag and try to "pass" it in via the docker file, but it does not work (error: This site can’t be reached)
Not working:
docker run -d -v /dv/sm:/dv/sm sa:latest
I've tried -
Docker file:
FROM WorkingTestImage as MyImage
ENTRYPOINT "/opt/bin/entry_point.sh"
CMD ["-p","5555:4444"]
FROM WorkingTestImage as MyImage
ENTRYPOINT ["/opt/bin/entry_point.sh","-p","5555:4444"]
CMD ["-p","5555:4444"]
FROM WorkingTestImage as MyImage
ENTRYPOINT ["/opt/bin/entry_point.sh","-p","5555:4444"]

The -p option is specifically used with the docker command meanwhile the CMD in Dockerfile just runs a command inside the docker container when it runs. So it is out of the scope from "docker" command.
If you want to write the port as a code you need to use Docker Compose or Kubernetes.

Related

docker basics: docker run -it with or without /bin/bash [duplicate]

This question already has answers here:
running docker container without /bin/bash command
(2 answers)
Closed 1 year ago.
I am self-studying docker basics and I am not sure what's the difference when we use "docker run -it centos" vs "docker run -it centos /bin/bash". I tested two commands and both of them seem to start a centos terminal.
See next:
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
The /bin/bash after image is called COMMAND, but for centos, the default command already defined in Dockerfile, see this:
FROM scratch
ADD centos-8-x86_64.tar.xz /
LABEL org.label-schema.schema-version="1.0" org.label-schema.name="CentOS Base Image" org.label-schema.vendor="CentOS" org.label-schema.license="GPLv2" org.label-schema.build-date="20201204"
CMD ["/bin/bash"]
So, the situation you mentioned above same here.

Docker container exits as soon as I start it

When I run or start a Docker container, it will not stay running.
Docker start will just return the name of whatever container I gave it, but wont actually do anything. Docker run (ex $ docker run -p 8080:80 --name hello -d hello-world) will create it but it will exit immediately.
If I run docker ps after one of these, it will show nothing listed as currently running.
If I run docker ps -a, it will show all of my containers and show the one that I just attempted to run having exited a few seconds ago.
Is this common and how do I get my containers to stay running? I am trying to learn how to use Docker and it has been one of the worst experiences. Thank you for any help or suggestions
Docker containers are generally used to run applications/processes in an isolated environment.
When you run the hello-world image, it creates a container which has only purpose of printing out the name using standard output. That is the only process that ran and the container was done with its work. That is why you see nothing when done docker ps.
In order to keep a container running, you need to have a process inside that container that will run (for example: a server, database, application etc.)
Try creating a container form mysql image, and then check the running container.
In your command, you specify the -d flag (aka detach), which means Run container in background and print container ID (from Docker docs). See more discussion about this here: Docker container will automatically stop after "docker run -d"
docker run -p 8080:80 --name hello -d hello-world
If you run it without the -d flag, it should run in the foreground and send output to your terminal
docker run -p 8080:80 --name hello hello-world
You don't see it running in docker ps -a because that container just executes the hello-world script and exits. If the container starts a long running process then you'll be able to find it in docker ps -a. To verify this, you can try running the nginx demo containers (e.g. nginx-hello) which serve up 'hello world'/demo pages.
To know what's wrong with your container use (docker logs (your container name)) command.
then you can sort it out what went wrong with your container
Is this common and how do I get my containers to stay running?
What happen when you start a Docker container ?
By default, it executes the command/the entrypoint specified in the Dockerfile image.
Generally that command or the entrypoint is a script or a program located in the image.
When that script/program exits, the container exits too. That's all.
To keep a container alive, the script/program has to stay running.
You start an hello image container, a "hello" container says "hello" and exits.
That may be a script as simple as :
#!/bin/sh
echo "hello"
So that is expected to finish and exit the container.
Run a database or a web server and you will see a different behavior. The script/program keeps running... while you don't stop that. So the container also stays running while you don't stop that.
To experiment, you can run your hello-world container with an endless command :
docker run -p 8080:80 --name hello -d hello-world --entrypoint tail -f /dev/null
You will see that the container stays running.
A docker container exits when its main process finishes. The hello-world main process just prints some text and exits, so container exits too.
You can run this command straightly to see it's text:
docker run hello-world
If you want a running container, maybe you can try a nginx demo:
docker run --name nginx-demo -p 8080:80 -d nginx
then you can visit http://localhost:8080 using your web browser.

Docker Container goes to stop state immediately

Here is my dockerfile
FROM httpd:latest
ENV ENV_VARIABLE "http://localhost:8081"
# COPY BUILD AND CONFIGURATION FILES
COPY entrypoint.sh /
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
Here is the entrypoint.sh file
#!/bin/bash
sed -i 's,ENV_VARIABLE,'"$ENV_VARIABLE"',g' /path/to/config/file
exec "$#"
To run the container
docker run -e ENV_VARIABLE=some-value <image-name>
The sed command works perfectly fine and the value from environment variable gets reflected in config file. But whenever i start the container the container stops automatically.
I ran the command docker logs to check the logs but logs were empty.
The Dockerfile reference notes:
If CMD is defined from the base image, setting ENTRYPOINT will reset CMD to an empty value. In this scenario, CMD must be defined in the current image to have a value.
So you need to find the CMD from the base image and repeat it in your Dockerfile. Among other places, you can find that on the Docker hub listing of the image history
CMD ["httpd-foreground"]
docker inspect httpd or docker history httpd would also be able to tell you this.

Use container environment variable in docker run without using bash -c

I have a WP-CLI container where I have to run the following command:
wp --allow-root core config --dbname=$MYSQL_DATABASE --dbuser=$MYSQL_USER --dbpass=$MYSQL_PASSWORD --dbhost=$WP_CLI_MYSQL_HOST --debug
When I run in bash inside container, I have no problem, but when I try to do:
docker-compose run --rm wordpress-cli --rm core config --dbname=$MYSQL_DATABASE --dbuser=$MYSQL_USER --dbpass=$MYSQL_PASSWORD --dbhost=$WP_CLI_MYSQL_HOST --allow-root --debug
All environment variables are evaluated in the host instead of the container, so they are passed empty to container.
I found in this question, that using bash -c 'my command' will do the trick, but my ENTRYPOINT is the WP command, so I want to just run without using the bash command.
Just escape the $ so they get passed through to the container:
docker-compose run --rm wordpress-cli --rm core config --dbname=\$MYSQL_DATABASE --dbuser=\$MYSQL_USER ...

Docker. How to get bash\ssh inside runned container (run -d)?

I want to ssh or bash into runned docker container. Please, see example:
$ sudo docker run -d webserver
webserver is clean image from ubuntu:14.04
$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
665b4a1e17b6 webserver:latest /bin/bash ... ... 22/tcp, 80/tcp loving_heisenberg
now I want to get something like this (go into runned container):
$ sudo docker run -t -i webserver (or maybe 665b4a1e17b6 instead)
$ root#665b4a1e17b6:/#
Previously I used Vagrant so I want to get behavior similar to vagrant ssh. Please, could anyone help me?
After the release of Docker version 1.3, the correct way to get a shell or other process on a running container is using the docker exec command. For example, you would run the following to get a shell on a running container:
docker exec -it myContainer /bin/bash
You can find more information in the documentation.
The answer is docker attach command.
For information see: https://askubuntu.com/a/507009/159189

Resources