I am using Memgraph with Docker and I want to access today's log files. What is the easiest way to do that?
To view the log files you first need to enter the Docker container where Memgraph is running. Then follow these steps:
Execute the command docker ps
to find the ID of the container, CONTAINER_ID.
Then, execute the command
docker exec -it CONTAINER_ID bash
Now you can use the command
cat /var/log/memgraph/memgraph_2022-03-03.log
to check today's log file.
Related
so I have a working Rails project which is running in a docker container
I have a need to use this tool : https://github.com/google/oauth2l
I need to use this tool within my rails application, take it's output and use it in a post request I'm making via HTTParty.
I'm able to do this already but that's assuming I have oauth2l installed on my system. But since
I'm running my app in production in a container, I'm not sure what the best way to go about this is.
In the docs, there does seem to be a way to 'inject' it into my container but adding those lines to the Dockerfile is leading to syntax errors.
Any ideas on what I could do here?
As a general rule, I think you shouldn't run commands inside the docker container unless you really need it, the best approach is to install what you need while you are building your docker image (with only docker or docker-compose), but in case you really need to enter inside your container here is how I usually do it:
sudo -s
docker container ls (list all containers)
docker container -ti <id of the container> sh
or
docker exec -ti <id of the container> /bin/bash
The sudo -s sometimes is needed depending on how you configure the instance where your container is running, which means that all the commands you are running from now on are like admin. And you may need to execute sh or bash, again depending on your instance.
Hope that helps! 👍
Is better to type all the commands needed in the Dockerfile.
BUt, if you want to run a command within the container, try this:
docker exec -it my-app-web-1 rails db:create db:migrate
docker exec -it my-app-web-1 rails devise:install
These are some examples that you can do.
I have created a docker container which is up and running in the background. I tried to attach the container in my terminal using the container id. It enters the container but I can't do anything in it. So, I have to exit every time using ctrl+c.
If the comment by Lawrence Cherone did not help you consider the following options:
If you want to invoke a command in a already running container, use docker exec.
You can exec into you container by it's name or id. This means you can run a command in a running container. See docs.docker.com: docker exec.
Example:
The docker exec command runs the command bash in a running container.
docker exec -it <container-id> bash
Note: This will really invoke a new command and do not attach to the currelty running command.
If you just want to get the putput of a container, you can use docker logs. Again you use your container name or id.
See docs.docker.com: docker logs
Example:
docker logs <container-id>
What is the difference between docker run and docker create commands?
I usually use run but sometimes in documentation I see create.
Docker's --help tells
create Create a new container
run Run a command in a new container
Does it mean that run is used when we need to pass a command to a new container? What's the aim of create then?
docker run = docker create + docker start.
From docker documentation
The docker create command creates a writeable container layer over the
specified image and prepares it for running the specified command. The
container ID is then printed to STDOUT. This is similar to docker run
-d except the container is never started. You can then use the docker start command to start the container at any point.
This is useful when you want to set up a container configuration ahead
of time so that it is ready to start when you need it. The initial
status of the new container is created.
docker create command creates a writeable container from the image and prepares it for running.
docker run command creates the container (same as docker create) and starts it.
The other answers have this covered but I thought I'd show the equivalent shell command-lines because it makes it really clear:
$ docker run myimage
is the same as
$ docker start -a $(docker create myimage)
Here, docker create is used to create a container from the named image and outputs the created container id and docker start is used to start the container with that id. The -a option causes the terminal to attach so that the container runs in the foreground which is the default behaviour of docker run.
A container that has been created but never started will have a Created status; this can be seen with docker container ls -a.
I'm new to docker and just got around to playing with it;
My take is that docker run essentially does the following: (in the order of..) docker create, docker start, docker attach , since it immediately attaches to the active shell after you do the 'run' command.
to create a container:
to start a container:
to create and start with a single command:
Now to understand we must dig deep with create and start.
Process of creating a container is taking the file system from image, and kind of prep it for use in the new container. When we create the container we are just prepping or setting up the file system snapshot to be used to create the container to actually start the container.
So creating container is about the file system starting it is about actually executing the startup the command.
And to start the container, we actually execute the start up command that might start up the process.
Lets see it in terminal:
When I run command "sudo docker create hello-world" it prints bellow output.
In the output we saw characters printed out. This is the ID of the container that was just created, Now I can actually execute the hello world command inside of this container by running Docker start.
So what happened here, first off we kind of prop the container by getting the file system ready.
Then after that we actually executed the primary start up command in there with Docker start.
-a in the docker start command is for watching output from the container and print it out to your terminal.
So there is very small difference between Docker run and docker start, by default Docker run is going to show you all the logs or all the information coming out of the container. By default Docker start is the opposite Docker start is not going to show you information coming out of the terminal.
Now you know when you need to use Run / Create / Start
Docker run is basically for running commands in the container.
docker run -it <Container Name> /bin/bash
The above is for creating a bash terminal. And make us use bash commands in the container.
Docker create is to create a container from an Docker Image.
docker create -d /var/lib:/var/lib --name docker-ubuntu ubuntu
The above is to create a docker a container of the name "docker-ubuntu" from the image "ubuntu"
In practice to start a container I do:
docker run a8asd8f9asdf0
If thats the case, what does:
docker start
do?
In the manual it says
Start one or more stopped containers
This is a very important question and the answer is very simple, but fundamental:
Run: create a new container of an image, and execute the container. You can create N clones of the same image. The command is:
docker run IMAGE_ID and not docker run CONTAINER_ID
Start: Launch a container previously stopped. For example, if you had stopped a database with the command docker stop CONTAINER_ID, you can relaunch the same container with the command docker start CONTAINER_ID, and the data and settings will be the same.
run runs an image
start starts a container.
The docker run doc does mention:
The docker run command first creates a writeable container layer over the specified image, and then starts it using the specified command.
That is, docker run is equivalent to the API /containers/create then /containers/(id)/start.
You do not run an existing container, you docker exec to it (since docker 1.3).
You can restart an exited container.
Explanation with an example:
Consider you have a game (iso) image in your computer.
When you run (mount your image as a virtual drive), a virtual drive is created with all the game contents in the virtual drive and the game installation file is automatically launched. [Running your docker image - creating a container and then starting it.]
But when you stop (similar to docker stop) it, the virtual drive still exists but stopping all the processes. [As the container exists till it is not deleted]
And when you do start (similar to docker start), from the virtual drive the games files start its execution. [starting the existing container]
In this example - The game image is your Docker image and virtual drive is your container.
run command creates a container from the image and then starts the root process on this container. Running it with run --rm flag would save you the trouble of removing the useless dead container afterward and would allow you to ignore the existence of docker start and docker remove altogether.
run command does a few different things:
docker run --name dname image_name bash -c "whoami"
Creates a Container from the image. At this point container would have an id, might have a name if one is given, will show up in docker ps
Starts/executes the root process of the container. In the code above that would execute bash -c "whoami". If one runs docker run --name dname image_name without a command to execute container would go into stopped state immediately.
Once the root process is finished, the container is stopped. At this point, it is pretty much useless. One can not execute anything anymore or resurrect the container. There are basically 2 ways out of stopped state: remove the container or create a checkpoint (i.e. an image) out of stopped container to run something else. One has to run docker remove before launching container under the same name.
How to remove container once it is stopped automatically? Add an --rm flag to run command:
docker run --rm --name dname image_name bash -c "whoami"
How to execute multiple commands in a single container? By preventing that root process from dying. This can be done by running some useless command at start with --detached flag and then using "execute" to run actual commands:
docker run --rm -d --name dname image_name tail -f /dev/null
docker exec dname bash -c "whoami"
docker exec dname bash -c "echo 'Nnice'"
Why do we need docker stop then? To stop this lingering container that we launched in the previous snippet with the endless command tail -f /dev/null.
daniele3004's answer is already pretty good.
Just a quick and dirty formula for people like me who mixes up run and start from time to time:
docker run [...] = docker pull [...] + docker start [...]
It would have been wiser to name the command "new" instead of "run".
Run creates a container instance of an existing (or downloadable) image and starts it.
I have only one docker instance running. My actual task to write shell script around this container.
However the first step is to get the container ID via a shell command.
Is there a shell command to do it?
you can do either
docker ps -q
or also, as the only running container is also the last one
docker ps -lq